home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / manage / snmp / cmu-snmp1.0 / apps / snmptrapd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-19  |  5.6 KB  |  224 lines

  1. /*
  2.  * snmptrapd.c - receive and log snmp traps
  3.  *
  4.  */
  5. /***********************************************************
  6.     Copyright 1989 by Carnegie Mellon University
  7.  
  8.                       All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the name of CMU not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25. ******************************************************************/
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <stdio.h>
  29. #include <sys/time.h>
  30. #include <errno.h>
  31. #include <syslog.h>
  32.  
  33. #include "snmp.h"
  34. #include "snmp_impl.h"
  35. #include "asn1.h"
  36. #include "snmp_api.h"
  37. #include "snmp_client.h"
  38.  
  39. #ifndef BSD4_3
  40.  
  41. typedef long    fd_mask;
  42. #define NFDBITS    (sizeof(fd_mask) * NBBY)    /* bits per mask */
  43.  
  44. #define    FD_SET(n, p)    ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
  45. #define    FD_CLR(n, p)    ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
  46. #define    FD_ISSET(n, p)    ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
  47. #define FD_ZERO(p)    bzero((char *)(p), sizeof(*(p)))
  48. #endif
  49.  
  50. extern int  errno;
  51. int    snmp_dump_packet = 0;
  52. int Print = 0;
  53.  
  54. char *
  55. trap_description(trap)
  56.     int trap;
  57. {
  58.     switch(trap){
  59.     case SNMP_TRAP_COLDSTART:
  60.         return "Cold Start";
  61.     case SNMP_TRAP_WARMSTART:
  62.         return "Warm Start";
  63.     case SNMP_TRAP_LINKDOWN:
  64.         return "Link Down";
  65.     case SNMP_TRAP_LINKUP:
  66.         return "Link Up";
  67.     case SNMP_TRAP_AUTHFAIL:
  68.         return "Authentication Failure";
  69.     case SNMP_TRAP_EGPNEIGHBORLOSS:
  70.         return "EGP Neighbor Loss";
  71.     case SNMP_TRAP_ENTERPRISESPECIFIC:
  72.         return "Enterprise Specific";
  73.     default:
  74.         return "Unknown Type";
  75.     }
  76. }
  77.  
  78. char *
  79. uptime_string(timeticks, buf)
  80.     register u_long timeticks;
  81.     char *buf;
  82. {
  83.     int    seconds, minutes, hours, days;
  84.  
  85.     timeticks /= 100;
  86.     days = timeticks / (60 * 60 * 24);
  87.     timeticks %= (60 * 60 * 24);
  88.  
  89.     hours = timeticks / (60 * 60);
  90.     timeticks %= (60 * 60);
  91.  
  92.     minutes = timeticks / 60;
  93.     seconds = timeticks % 60;
  94.  
  95.     if (days == 0){
  96.     sprintf(buf, "%d:%02d:%02d", hours, minutes, seconds);
  97.     } else if (days == 1) {
  98.     sprintf(buf, "%d day, %d:%02d:%02d", days, hours, minutes, seconds);
  99.     } else {
  100.     sprintf(buf, "%d days, %d:%02d:%02d", days, hours, minutes, seconds);
  101.     }
  102.     return buf;
  103. }
  104.  
  105. int snmp_input(op, session, reqid, pdu, magic)
  106.     int op;
  107.     struct snmp_session *session;
  108.     int reqid;
  109.     struct snmp_pdu *pdu;
  110.     void *magic;
  111. {
  112.     char buf[64];
  113.  
  114.     if (op == RECEIVED_MESSAGE && pdu->command == TRP_REQ_MSG){
  115.     if (Print){
  116.         printf("%s: %s Trap (%d) Uptime: %s\n", inet_ntoa(pdu->agent_addr.sin_addr),
  117.         trap_description(pdu->trap_type), pdu->specific_type, uptime_string(pdu->time, buf));
  118.     } else {
  119.         syslog(LOG_WARNING, "%s: %s Trap (%d) Uptime: %s\n", inet_ntoa(pdu->agent_addr.sin_addr),
  120.         trap_description(pdu->trap_type), pdu->specific_type, uptime_string(pdu->time, buf));
  121.     }
  122.     } else if (op == TIMED_OUT){
  123.     printf("Timeout: This shouldn't happen!\n");
  124.     }
  125. }
  126.  
  127.  
  128. main(argc, argv)
  129.     int        argc;
  130.     char    *argv[];
  131. {
  132.     struct snmp_session session, *ss;
  133.     int    arg;
  134.     int count, numfds, block;
  135.     fd_set fdset;
  136.     struct timeval timeout, *tvp;
  137.  
  138.  
  139.     init_syslog();
  140.     /*
  141.      * usage: snmptrapd [-p]
  142.      */
  143.     for(arg = 1; arg < argc; arg++){
  144.     if (argv[arg][0] == '-'){
  145.         switch(argv[arg][1]){
  146.         case 'd':
  147.             snmp_dump_packet++;
  148.             break;
  149.         case 'p':
  150.             Print++;
  151.             break;
  152.         default:
  153.             printf("invalid option: -%c\n", argv[arg][1]);
  154.             printf("Usage: snmptrapd [-p ]\n");
  155.             break;
  156.         }
  157.         continue;
  158.     }
  159.     }
  160.  
  161.     bzero((char *)&session, sizeof(struct snmp_session));
  162.     session.peername = NULL;
  163.     session.community = NULL;
  164.     session.community_len = 0;
  165.     session.retries = SNMP_DEFAULT_RETRIES;
  166.     session.timeout = SNMP_DEFAULT_TIMEOUT;
  167.     session.authenticator = NULL;
  168.     session.callback = snmp_input;
  169.     session.callback_magic = NULL;
  170.     session.local_port = SNMP_TRAP_PORT;
  171.     ss = snmp_open(&session);
  172.     if (ss == NULL){
  173.     printf("Couldn't open snmp\n");
  174.     exit(-1);
  175.     }
  176.  
  177.     while(1){
  178.     numfds = 0;
  179.     FD_ZERO(&fdset);
  180.     block = 1;
  181.     tvp = &timeout;
  182.     timerclear(tvp);
  183.     snmp_select_info(&numfds, &fdset, tvp, &block);
  184.     if (block == 1)
  185.         tvp = NULL;    /* block without timeout */
  186.     count = select(numfds, &fdset, 0, 0, tvp);
  187.     if (count > 0){
  188.         snmp_read(&fdset);
  189.     } else switch(count){
  190.         case 0:
  191.         snmp_timeout();
  192.         break;
  193.         case -1:
  194.         if (errno == EINTR){
  195.             continue;
  196.         } else {
  197.             perror("select");
  198.         }
  199.         return -1;
  200.         default:
  201.         printf("select returned %d\n", count);
  202.         return -1;
  203.     }
  204.     }
  205. }
  206.  
  207. init_syslog(){
  208. /*
  209.  * These definitions handle 4.2 systems without additional syslog facilities.
  210.  */
  211. #ifndef LOG_CONS
  212. #define LOG_CONS    0    /* Don't bother if not defined... */
  213. #endif
  214. #ifndef LOG_LOCAL0
  215. #define LOG_LOCAL0    0
  216. #endif
  217.     /*
  218.      * All messages will be logged to the local0 facility and will be sent to
  219.      * the console if syslog doesn't work.
  220.      */
  221.     openlog("snmptrapd", LOG_CONS, LOG_LOCAL0);
  222.     syslog(LOG_INFO, "Starting snmptrapd");
  223. }
  224.